﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using SayDemo.Data;
using SayDemo.Models;
using SayDemo.ViewModel;

namespace SayDemo.Controllers
{
    public class MessagesController : Controller
    {
        private SayDemoDb db = new SayDemoDb();

        // GET: Messages
        public ActionResult Index(int id)
        {
            var currentUser = db.Users.Where(x => x.Id == id).SingleOrDefault();

            //获得所有的用户
            var userList = db.Users.ToList();
            //获得所有的说说
            var msgList = db.Messages.ToList();
            //所有的评论
            var comList = db.Comments.ToList();

            //定义一个准备用来放全部首页消息模型的合集
            var msgViewModelList = new List<MessageViewModel>();
            var comViewModelList = new List<CommnetViewModel>();

            //遍历所有的消息啊
            foreach(var msg in msgList)
            {
                //定义一个合集，用来存放当前消息下的所有的评论
                var tempList = new List<CommnetViewModel>();

                //根据说说的id查找所有的评论
                var currentMsgComments = comList.Where(x => x.MsgId == msg.Id).ToList();

                //遍历当前说说的所有评论   
                foreach(var c in currentMsgComments)
                {
                    tempList.Add(new CommnetViewModel
                    {
                        //根据当前评论的用户id，在用户查找对于的用户，获得用户的名字
                        FormUserName=userList.SingleOrDefault(x=>x.Id==c.FromUserId).Username,
                        //
                        ToUserName =userList.SingleOrDefault(x => x.Id == msgList.SingleOrDefault(t=>t.Id==c.MsgId).FormUserId).Username,
                        Comment=c.Content
                    });
                }
                //当前说说转换为首页说说视图模型
                msgViewModelList.Add(new MessageViewModel
                {
                    Id = msg.Id,
                    FormUserId = msg.FormUserId,
                    Content = msg.Content,
                    Comments= tempList
                });
            }

            var data = new SayViewModel
            {
                Username = currentUser.Username,
                Messages =msgViewModelList
                
            };
            return View(data);
        }

        // GET: Messages/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Messages messages = db.Messages.Find(id);
            if (messages == null)
            {
                return HttpNotFound();
            }
            return View(messages);
        }

        // GET: Messages/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Messages/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性。有关
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,FormUserId,Content,CreatedAt,UpdatedAt,Version,Remarks")] Messages messages)
        {
            if (ModelState.IsValid)
            {
                db.Messages.Add(messages);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(messages);
        }

        // GET: Messages/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Messages messages = db.Messages.Find(id);
            if (messages == null)
            {
                return HttpNotFound();
            }
            return View(messages);
        }

        // POST: Messages/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性。有关
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,FormUserId,Content,CreatedAt,UpdatedAt,Version,Remarks")] Messages messages)
        {
            if (ModelState.IsValid)
            {
                db.Entry(messages).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(messages);
        }

        // GET: Messages/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Messages messages = db.Messages.Find(id);
            if (messages == null)
            {
                return HttpNotFound();
            }
            return View(messages);
        }

        // POST: Messages/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Messages messages = db.Messages.Find(id);
            db.Messages.Remove(messages);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
